
clear
set more off

cd "D:\data_replication"


// Construct shares for all aggregation levels
//==============================================================================

use estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc8_gini.dta, clear
tostring pc8, replace
gen ll = length(pc8)
gen pc7 = pc8
replace pc7 = substr(pc8, 1, 7) if ll == 8
sort year quarter declarant pc7
forval i = 1/100 {
display `i'
by year quarter declarant pc7: egen s_`i'_pc7 = sum(s_`i')
}
by year quarter declarant pc7: keep if _n == 1
drop if ll != 8
keep year quarter declarant pc7 s_1_pc7-s_100_pc7
save estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc7_gini.dta, replace


use estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc8_gini.dta, clear
tostring pc8, replace
gen ll = length(pc8)
gen pc6 = pc8
replace pc6 = substr(pc8, 1, 6) if ll == 8
sort year quarter declarant pc6
forval i = 1/100 {
display `i'
by year quarter declarant pc6: egen s_`i'_pc6 = sum(s_`i')
}
by year quarter declarant pc6: keep if _n == 1
drop if ll != 8
keep year quarter declarant pc6 s_1_pc6-s_100_pc6
save estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc6_gini.dta, replace


use estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc8_gini.dta, clear
tostring pc8, replace
gen ll = length(pc8)
gen pc5 = pc8
replace pc5 = substr(pc8, 1, 5) if ll == 8
sort year quarter declarant pc5
forval i = 1/100 {
display `i'
by year quarter declarant pc5: egen s_`i'_pc5 = sum(s_`i')
}
by year quarter declarant pc5: keep if _n == 1
drop if ll != 8
keep year quarter declarant pc5 s_1_pc5-s_100_pc5
save estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc5_gini.dta, replace


use estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc8_gini.dta, clear
tostring pc8, replace
gen ll = length(pc8)
gen pc4 = pc8
replace pc4 = substr(pc8, 1, 4) if ll == 8
sort year quarter declarant pc4
forval i = 1/100 {
display `i'
by year quarter declarant pc4: egen s_`i'_pc4 = sum(s_`i')
}
by year quarter declarant pc4: keep if _n == 1
drop if ll != 8
keep year quarter declarant pc4 s_1_pc4-s_100_pc4
save estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc4_gini.dta, replace


use estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc8_gini.dta, clear
tostring pc8, replace
gen ll = length(pc8)
gen pc3 = pc8
replace pc3 = substr(pc8, 1, 3) if ll == 8
sort year quarter declarant pc3
forval i = 1/100 {
display `i'
by year quarter declarant pc3: egen s_`i'_pc3 = sum(s_`i')
}
by year quarter declarant pc3: keep if _n == 1
drop if ll != 8
keep year quarter declarant pc3 s_1_pc3-s_100_pc3
save estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc3_gini.dta, replace


use estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc8_gini.dta, clear
tostring pc8, replace
gen ll = length(pc8)
gen pc2 = pc8
replace pc2 = substr(pc8, 1, 2) if ll == 8
sort year quarter declarant pc2
forval i = 1/100 {
display `i'
by year quarter declarant pc2: egen s_`i'_pc2 = sum(s_`i')
}
by year quarter declarant pc2: keep if _n == 1
drop if ll != 8
keep year quarter declarant pc2 s_1_pc2-s_100_pc2
save estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc2_gini.dta, replace


use estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc8_gini.dta, clear
tostring pc8, replace
rename pc8 pc7
forval i = 1/100 {
rename s_`i' s_`i'_pc7
}
append using estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc7_gini.dta
rename pc7 pc6
forval i = 1/100 {
rename s_`i'_pc7 s_`i'_pc6
}
append using estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc6_gini.dta
rename pc6 pc5
forval i = 1/100 {
rename s_`i'_pc6 s_`i'_pc5
}
append using estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc5_gini.dta
rename pc5 pc4
forval i = 1/100 {
rename s_`i'_pc5 s_`i'_pc4
}
append using estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc4_gini.dta
rename pc4 pc3
forval i = 1/100 {
rename s_`i'_pc4 s_`i'_pc3
}
append using estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc3_gini.dta
rename pc3 pc2
forval i = 1/100 {
rename s_`i'_pc3 s_`i'_pc2
}
append using estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc2_gini.dta
rename pc2 pc
forval i = 1/100 {
rename s_`i'_pc2 s_`i'_pc
}
save estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc_gini.dta, replace


// Compute shares for markets and product ids
//==============================================================================

// 2003
//------------------------------------------------------------------------------

forval yy = 2003/2003 {
forval qq = 1/4 {
foreach dd in 1 3 4 5 6 7 8 9 10 11 17 18 30 32 38 {

use estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc_gini.dta, clear
keep if year == `yy' & quarter == `qq' & declarant == `dd'
save estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc_temp_gini.dta, replace

use estimation\2_product_list\output\product_id_all.dta, clear
merge m:1 pc using estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc_temp_gini.dta
sort product_id pc8plus
drop if _merge == 2
forval i = 1/100 {
replace s_`i'_pc = 0 if s_`i'_pc == .
}

drop _merge
forval i = 1/100 {
rename s_`i'_pc s_`i'_pc_main
}

rename pc pc_main
rename pc8plus pc
merge m:1 pc using estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc_temp_gini.dta
drop if _merge == 2
sort product_id pc
forval i = 1/100 {
replace s_`i'_pc = 0 if s_`i'_pc == . & _n > 3817
}



sort product_id
forval i = 1/100 {
by product_id: egen s_`i'_pc_sub = sum(s_`i'_pc)
}
by product_id: keep if _n == 1

forval i = 1/100 {
gen s_`i'_product_id = s_`i'_pc_main + s_`i'_pc_sub
}
keep year quarter declarant product_id s_1_product_id-s_100_product_id

replace year = `yy' if year == .
replace quarter = `qq' if quarter == .
replace declarant = `dd' if declarant == .

save estimation\4_demand_estimation\4_cobb_douglas_weights\weights_product_id_`yy'_`qq'_`dd'_gini.dta, replace

}
}
}


// 2004-2005
//------------------------------------------------------------------------------

forval yy = 2004/2005 {
forval qq = 1/4 {
foreach dd in 1 3 4 5 6 7 8 9 10 11 17 18 30 32 38 46 53 54 55 60 61 63 64 66 68 91 600 {

use estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc_gini.dta, clear
keep if year == `yy' & quarter == `qq' & declarant == `dd'
save estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc_temp_gini.dta, replace

use estimation\2_product_list\output\product_id_all.dta, clear
merge m:1 pc using estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc_temp_gini.dta
sort product_id pc8plus
drop if _merge == 2
forval i = 1/100 {
replace s_`i'_pc = 0 if s_`i'_pc == .
}


drop _merge
forval i = 1/100 {
rename s_`i'_pc s_`i'_pc_main
}
rename pc pc_main
rename pc8plus pc
merge m:1 pc using estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc_temp_gini.dta
drop if _merge == 2
sort product_id pc
forval i = 1/100 {
replace s_`i'_pc = 0 if s_`i'_pc == . & _n > 3817
}


sort product_id
forval i = 1/100 {
by product_id: egen s_`i'_pc_sub = sum(s_`i'_pc)
}
by product_id: keep if _n == 1

forval i = 1/100 {
gen s_`i'_product_id = s_`i'_pc_main + s_`i'_pc_sub
}
keep year quarter declarant product_id s_1_product_id-s_100_product_id

replace year = `yy' if year == .
replace quarter = `qq' if quarter == .
replace declarant = `dd' if declarant == .

save estimation\4_demand_estimation\4_cobb_douglas_weights\weights_product_id_`yy'_`qq'_`dd'_gini.dta, replace

}
}
}


// Collect results and save
//==============================================================================

use estimation\4_demand_estimation\4_cobb_douglas_weights\weights_product_id_2003_1_1_gini.dta, clear
gen temp = 1

forval yy = 2003/2003 {
forval qq = 1/4 {
foreach dd in 1 3 4 5 6 7 8 9 10 11 17 18 30 32 38 {

append using estimation\4_demand_estimation\4_cobb_douglas_weights\weights_product_id_`yy'_`qq'_`dd'_gini.dta
rm estimation\4_demand_estimation\4_cobb_douglas_weights\weights_product_id_`yy'_`qq'_`dd'_gini.dta

}
}
}


forval yy = 2004/2005 {
forval qq = 1/4 {
foreach dd in 1 3 4 5 6 7 8 9 10 11 17 18 30 32 38 46 53 54 55 60 61 63 64 66 68 91 600 {

append using estimation\4_demand_estimation\4_cobb_douglas_weights\weights_product_id_`yy'_`qq'_`dd'_gini.dta
rm estimation\4_demand_estimation\4_cobb_douglas_weights\weights_product_id_`yy'_`qq'_`dd'_gini.dta

}
}
}

drop if temp == 1
drop temp
save estimation\4_demand_estimation\4_cobb_douglas_weights\weights_product_id_gini.dta, replace


rm estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc_gini.dta
rm estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc_temp_gini.dta
rm estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc2_gini.dta
rm estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc3_gini.dta
rm estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc4_gini.dta
rm estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc5_gini.dta
rm estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc6_gini.dta
rm estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc7_gini.dta

